package com.example.speeddating.controller;

import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Preference;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.PreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-08-05 03:18:43
 */
@RestController
@RequestMapping("/preference")
public class PreferenceController {

    @Autowired
    private PreferenceService preferenceService;

    @TokenCheck(roles = "user")
    @PostMapping("/add")
    public BaseResponse addPreference(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("cityType")==null||jsonString.get("minAge")==null||jsonString.get("maxAge")==null||jsonString.get("minHeight")==null||jsonString.get("maxHeight")==null||jsonString.get("education")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        Preference preference = new Preference();
        preference.setUserId(tokenEntity.getId());
        preference.setCityType(Integer.valueOf(jsonString.get("cityType").toString()));
        preference.setMinAge(Integer.valueOf(jsonString.get("minAge").toString()));
        preference.setMaxAge(Integer.valueOf(jsonString.get("maxAge").toString()));
        preference.setMinHeight(Double.valueOf(jsonString.get("minHeight").toString()));
        preference.setMaxHeight(Double.valueOf(jsonString.get("maxHeight").toString()));
        preference.setEducation(jsonString.get("education").toString());
        preferenceService.updatePreference(preference);
        return RespGenerator.returnOK("添加成功");
    }

    @TokenCheck(roles = "user")
    @PostMapping("/reset")
    public BaseResponse addPreference(@LoginUser TokenEntity tokenEntity){
        preferenceService.resetPreference(tokenEntity.getId());
        return RespGenerator.returnOK("重置成功");
    }


    @TokenCheck(roles = "user")
    @GetMapping("/select")
    public BaseResponse selectPreference(@LoginUser TokenEntity tokenEntity){
        Preference preference = preferenceService.selectPreferenceByUserId(tokenEntity.getId());
        return RespGenerator.returnOK(preference);
    }


}
